<script>
    let num = 2
    const goods = {
        price: 100,
        num: num
    }

    // 通过proxy来代理对象
    const proxyObj = new Proxy(goods,{
        get(target, key){
            console.log(`get ${key}`);
            return target[key]
        },
        set(target, key, value){
            console.log(`set ${key}`);
            target[key] = value
            effect()
        }
    })

    let total = 0
    const effect = () => {
        total = proxyObj.price * proxyObj.num
    }
    effect()
    console.log(`总价：${total}`);
    // Object.defineProperty(goods, "num",{
    //     get(){
    //         console.log("getter");
    //         return num
    //     },
    //     set(newValue){
    //         console.log("setter");
    //         num = newValue
    //         effect()
    //     }
    // })
    
</script>